﻿using RestSharp;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace RuoyiQuickLogin.Core
{
#if !NET472
#nullable enable
#endif
    public static class HttpUtils
    {
        public static string HttpPost(ref CookieContainer cookie, string Url, string postDataStr,
#if NET472
    Dictionary<string, string>
#else
    Dictionary<string, string>?
#endif
            headers = null,
#if NET472
    string
#else
    string?
#endif
            body = null
            )
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/json;charset=UTF-8";
            request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
            request.CookieContainer = cookie;

            Stream myRequestStream = request.GetRequestStream();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.UTF8);
            myStreamWriter.Write(postDataStr);
            myStreamWriter.Close();

            if (headers != null)
            {
                foreach (string key in headers.Keys)
                {
                    request.Headers.Add(key, headers[key]);
                }
            }

            if (body != null)
            {
                using (var dataStream = new StreamWriter(request.GetRequestStream()))
                {
                    dataStream.Write(body);
                }
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            response.Cookies = cookie.GetCookies(response.ResponseUri);

            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }

        public static string HttpRestPost(ref CookieContainer cookie, string Url,
#if NET472
    Dictionary<string, string>
#else
    Dictionary<string, string>?
#endif
            headers = null,
#if NET472
    string
#else
    string?
#endif
            body = null
    )
        {
            var client = new RestClient(Url);
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("sec-ch-ua", "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"96\", \"Google Chrome\";v=\"96\"");
            request.AddHeader("Accept", "application/json, text/plain, */*");
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("sec-ch-ua-mobile", "?0");
            client.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36";
            request.AddHeader("sec-ch-ua-platform", "\"Windows\"");

            if (headers != null)
            {
                foreach (var header in headers)
                {
                    request.AddHeader(header.Key, header.Value);
                }
            }

            if (body != null)
                request.AddParameter("application/json;charset=UTF-8", body, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);

            return response.Content;
        }

        public static string HttpGet(string Url, string postDataStr,
#if NET472
    Dictionary<string, string>
#else
    Dictionary<string, string>?
#endif
            headers = null
            )
        {
            var client = new RestClient(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            client.Timeout = -1;
            var request = new RestRequest(Method.GET);
            request.AddHeader("sec-ch-ua", "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"96\", \"Google Chrome\";v=\"96\"");
            request.AddHeader("Accept", "application/json, text/plain, */*");
            request.AddHeader("sec-ch-ua-mobile", "?0");

            if (headers != null)
            {
                foreach (var header in headers)
                {
                    request.AddHeader(header.Key, header.Value);
                }
            }

            client.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36";
            request.AddHeader("sec-ch-ua-platform", "\"Windows\"");
            IRestResponse response = client.Execute(request);

            return response.Content;
        }

    }
}
